/**
 * Created by xiewl on 2016/4/13.
 */
//http请求基础模块
function httpRequest(url,method,jsondata,callback){
    var urlto = urlParam.baseApiUrl + url;
    var timeStamp = $$.util.timeStamp();
    var sign = localStorage.sign || 'login';
    var token = localStorage.token || 'login';
    var phone = localStorage.phone;
    var ctype = urlParam.ctype;
    var usertype = urlParam.usertype;
    //内嵌页面从url参数中获取token
    var urlParamObj = $$.util.urlParamToObj();
    if(urlParamObj.phone && urlParamObj.token && urlParamObj.ctype && urlParamObj.usertype){
        $('body').addClass('M');
        token = urlParamObj.token;
        phone = urlParamObj.phone;
        ctype = urlParamObj.ctype;
        usertype = urlParamObj.usertype;
    }
    $.ajax({
        url : urlto,
        type:'post',
        data : {
            METHOD:method,
            PHONE:phone,
            SIGN:sign,
            TOKEN:token,
            RECDATA:jsondata,
            REQTIME:timeStamp,
            CTYPE:ctype,
            USERTYPE:usertype,
            VERSION:urlParam.clientversion
        },
        complete: function (XMLHttpRequest, textStatus) {
            //token失效
            if(XMLHttpRequest.responseText && JSON.parse(XMLHttpRequest.responseText).code == "10007"){
                location.href = "../../index.html?error=1";
                return false;
            }
            callback(XMLHttpRequest.responseText,textStatus);
        }
    });
}
//ajax请求类封装模块
function followupPlan(){
    var _this = this;
    var suid = localStorage.suid;
    //根据url分支获取项目基本信息
    _this.getProjectBaseInfo = function (urlBranch, callback) {
        var method = "wsuperprojectinfo";
        var sdata = JSON.stringify({
            "urlbranch":urlBranch
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //登录
    _this.login = function (phone,pwd,callback) {
        var method = "wlogin";
        //清空toekn
        localStorage.removeItem('token');
        localStorage.removeItem('sign');
        localStorage.phone = phone;
        var password = hex_md5(pwd);
        var sdata = JSON.stringify({
            "suid":suid,
            "phone":phone,
            "pwd":password
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //分类筛选(template 类型分类，creator 医生分类，hospital 医院分类)
    _this.classifyFilter = function (type,userid,callback) {
        var method = 'wselectionlist';
        var sdata = JSON.stringify({
            type : type,
            suid : suid,
            userid: userid
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取可创建随访模板列表
    _this.getTemplateList = function (callback) {
            var method = "wtemplatelist";
            var sdata = JSON.stringify({
                "doctor": JSON.parse(localStorage.userInfo).doctorId,
                "suid": suid
            });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //随访列表
    _this.goingFulist = function (doctorid,pageIndex,pageSize,sort,params,callback) {
        var method = 'wfollowuplist';
        var sdata = JSON.stringify({
            "suid":suid,
            "sort": sort,
            "page":pageIndex,
            "pagesize": pageSize,
            "doctorid" : doctorid,
            "params": JSON.stringify(params)
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //手机号匹配用户名
    _this.getPatientNameByPhone = function (phone, callback) {
        var method = 'getpatientname';
        var sdata = JSON.stringify({
            mobile : phone
        });
        httpRequest(urlParam.baseV1Url,method,sdata,callback)
    };
    //新建随访计划
    _this.createNewPlan = function (planModal, callback) {
        var method = 'wplansetrow';
        planModal.suid = suid;
        var sdata = JSON.stringify(planModal);
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取随访step
    _this.getFollowupSteps = function(planid,currentPage,pageSize,userType,callback){
        var method = "wfollowupsteps";
        var sdata = JSON.stringify({
            "planid" : planid,
            'currentPage' : currentPage,
            'pageSize' : pageSize,
            'userType' : userType
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //完成、取消随访计划（1:nomal,2:finish,3:cancel）
    _this.finishOrCancel = function(planid,status,callback){
        var method = "wfollowupfinishorcancel";
        var sdata = JSON.stringify({
            "planid":planid,
            "status":status
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取随访form列表
    _this.getFollowupForms = function (stepid, callback) {
        var method = 'wstepdata';
        var sdata = JSON.stringify({
            stepid : stepid
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //保存随访form列表
    _this.setFollowupForms = function (stepid,form,callback) {
        var method = 'wsavestepdata';
        var sdata = JSON.stringify({
            stepid : stepid,
            videos : '[]',
            pictures : '[]',
            form : JSON.stringify(form)
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取项目成员
    _this.getGroupMembers = function (callback) {
        var method = "wsuperprojectinfo";
        var sdata = JSON.stringify({
            "suid":suid
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取医生好友列表
    _this.getDoctorFriends = function (callback) {
        var method = "doctorfriendslist";
        var sdata = JSON.stringify({});
        httpRequest(urlParam.baseV1Duser,method,sdata,callback);
    };
    //添加项目成员
    _this.addGroupMembers = function (members,callback) {
        var method = "wsuperprojectinvitmember";
        var sdata = JSON.stringify({
            "id":suid,
            "members": JSON.stringify(members)
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //移除项目成员
    _this.removeGroupMembers = function (members,callback) {
        var method = "wsuperprojectremovemember";
        var sdata = JSON.stringify({
            "id":suid,
            "members": JSON.stringify(members)
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    };
    //获取静态资源真实路径
    _this.getRealSrc = function (srcurl,callback) {
        var method = 'wrealfileurl';
        var sdata = JSON.stringify({
            srcurl : srcurl
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback)
    };
    //搜索县级单位
    _this.searchCountyInfo = function (keyword,callback) {
        var method = 'wgetcountylike';
        var sdata = JSON.stringify({
            name : keyword
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback)
    };
    //上传静态资源文件
    _this.uploadFile = function (current,callback) {
        var sign = localStorage.sign || 'login';
        var token = localStorage.token || 'login';
        var phone = localStorage.phone;
        var ctype = urlParam.ctype;
        var usertype = urlParam.usertype;
        //内嵌页面从url参数中获取token
        var urlParamObj = $$.util.urlParamToObj();
        if(urlParamObj.phone && urlParamObj.token && urlParamObj.ctype && urlParamObj.usertype){
            token = urlParamObj.token;
            phone = urlParamObj.phone;
            ctype = urlParamObj.ctype;
            usertype = urlParamObj.usertype;
        }
        var url = urlParam.baseApiUrl + urlParam.baseV2Url +
            '?METHOD=wuploadfile&REQTIME='+$$.util.timeStamp()+'&SIGN='+ sign +'&TOKEN='+
            token +'&VERSION='+ urlParam.clientversion +'&CTYPE='+ ctype +'&PHONE='+ phone +
            "&USERTYPE=" + usertype + '&RECDATA={\"code\":\"followup\"}';
        var formdata = new FormData();
        var fileObj = current.files;
        for (var i = 0; i < fileObj.length; i++){
            formdata.append("web", fileObj[i]);
        }
        $.ajax({
            type: 'POST',
            url: url,
            data: formdata,
            contentType: false,
            processData: false,
            crossDomain: true,
            complete: function (XMLHttpRequest, textStatus) {
                callback(XMLHttpRequest.responseText,textStatus);
            }
        });
    };
    //变更参考节点时间
    _this.changeTime = function (planId,startTime, stepIndex, callback) {
        var method = "wfollowupchangetime";
        var sdata = JSON.stringify({
            "planid":planId,
            "starttime":startTime,
            "startstep":stepIndex
        });
        httpRequest(urlParam.baseV2Url,method,sdata,callback);
    }
}
